package Prova::FixedForms::SSLogin;

use strict;
use warnings;
use Constants;
use YAML qw//;

=head1 NAME

Prova::FixedForms::SSLogin - Prova::FixedForms Plugin

=head1 DESCRIPTION

none

=head1 METHODS

=cut

=head2 new

=cut

sub new {
    my $class = shift;
    my $self  = {};
    bless $self, $class;
}

=head2 type

=cut

sub type {
    my $self = shift;
    my $c    = shift;

    return (
        {
            type        => 'SSLogin',
            description => 'Pre login for Sm@rt Seminar 2.0'
        }
    );
}

=head2 execute

=cut

sub execute {
    my ( $self, $c, $testcase ) = @_;

    my %datablock = (
        1 => {
            block_path   => '/public/admin/login',
            block_method => Constants->METHOD_GET,
        },
        2 => {
            block_path             => '/public/admin/login/',
            block_method           => Constants->METHOD_POST,
            section_login_id       => 'admin',
            section_login_password => 'admin'
        },
        3 => {
            block_path   => '/seminar/list',
            block_method => Constants->METHOD_GET
        },
    );
    for my $i ( sort { $a <=> $b } keys %datablock ) {
        for my $j ( keys %{ $datablock{$i} } ) {
            $datablock{$i}->{$j} = YAML::Dump $datablock{$i}->{$j} if $j =~ /^section_(.*)/;
        }
        $c->stash->{'no_check'} = 1;
        $c->forward( '/recorder/default', [ $datablock{$i}, $testcase ] );
        $c->stash->{'no_check'} = 0;
    }

    my @datablocks = $c->model('DBIC::DataBlock')->search( { test_case_id => $testcase->id }, { order_by => 'order_by' } );
    for my $i (@datablocks) {
        my $order_by;
        for my $j ( sort { $a <=> $b } keys %datablock ) {
            if ( $i->path eq $datablock{$j}->{block_path} ) {
                $order_by = $j;
            }
        }
        $order_by = $i->order_by + scalar keys %datablock unless $order_by;
        $i->order_by($order_by);
        $i->update;
    }
}

=head1 AUTHOR

Shanon Inc, E<lt>http://www.shanon.co.jp/E<gt>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
